<template>
  <div>
        <el-dialog
      :title="isAdd ? '添加活动' : '编辑活动'"
      :visible.sync="isShow"
      :before-close="cancel"
      center

    >
      <el-form :model=" specsInfo" :rules="rules" ref=" specsInfo">
        
         <el-form-item
          label="活动名称"
          :label-width="formLabelWidth"
          prop="goodsname"
        >
          <el-input v-model=" specsInfo.goodsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="活动期限"
          :label-width="formLabelWidth"
          prop="price"
        >
          <el-input v-model="specsInfo.price" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="一级分类"
          :label-width="formLabelWidth"
          prop="first_cateid"
        >
          <el-select
            @change="changeCate(false)"
            v-model="specsInfo.first_cateid"
            placeholder="请选择分类"
          >
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="二级分类"
          :label-width="formLabelWidth"
          prop="second_cateid"
        >
          <el-select v-model="specsInfo.second_cateid"  @change="changeCate1" placeholder="请选择分类">
            <el-option
              v-for="item in catelist2"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

         <el-form-item
          label="商品"
          :label-width="formLabelWidth"
          prop="second_cateid"
        >
          <el-select v-model="specsInfo.goodscountInput" placeholder="请选择商品">
            <el-option
              v-for="item in goodscount"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
       
      
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="specsInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postSeckillDelete, getSpecsInfo, getSpecsEdit ,seckedit,catelist,getGoodsList} from "../../request/api";
import { mapActions } from "vuex";
export default {

  data() {
    return {
      goodscount:[],
      catelist:[],
      catelist2:[],
      secondArr:[],
      catelist:[],
      specsArr:[],
      specslist:[],
      dialogVisible:false,
      specsInfo: {
        specsname: "", //商品规格名称
        attrs: "", //规格属性值
        status: 1, //状态1正常2禁用
        goodscountInput:''
      },
      formLabelWidth: "120px", //label宽度
      //规则验证
      rules: {
        specsname: [
          { required: true, message: "请输入商品规格名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      //创建一个规格属性的数组
      attrArr: [
        {
          value: "",
          id: new Date().getTime(),
        },
      ],
    };
  },
  props: ["isShow", "isAdd"],
  methods: {
    ...mapActions({
      getSpecs: "specs/getSpecsListAction",
      getCount: "specs/getCountAction",
    }),
    confirm(){},
    changeSpecs(){},
    //封装一个新增事件
    addAttr() {
       this.goodscount=[];
       this.catelist2=[];
      //判断一下添加的个数
      if (this.attrArr.length < 6) {
        this.attrArr.push({
          value: "",
          id: new Date().getTime(),
        });
      } else {
        this.$message.warning("最多就添加6个，小宝贝！！！");
      }
    },
    //删除动态添加的表单项
    delAttr(i) {
      this.attrArr.splice(i, 1);
    },
    //封装一个取消事件
    cancel() {
      //去改变父组件的isShow的状态
      this.$emit("cancel", false);
      //调取清空事件
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.specsInfo = {
        specsname: "", //商品规格名称
        attrs: "", //规格属性值
        status: 1, //状态1正常2禁用
      };
      //调用清空验证的方法
      // this.$refs.specsInfo.resetFields();
      //清空动态表单项内容 回到初始状态
      this.attrArr = [
        {
          value: "",
          id: new Date().getTime(),
        },
      ];
    },
    // 确定事件
    addSpecs() {
      this.$refs.specsInfo.validate((valid) => {
        if (valid) {
          /* 
          我们现在获取出动态数据数组是 [{id:1,value:'值1'},{id:2,value:'值2'},{id:3,value:'值3'}....]
          后端要求的入参是：'值1,值2,值3...'
          可以通过map实现一个你想要的数组格式[值1,值2,值3...]
          */
          //  let result =this.attrArr.map(item=>{

          //    return item.value
          //  })
          //  console.log(result,'result');
          //   console.log(this.attrArr,'动态表单数据数组');
          this.specsInfo.attrs = this.attrArr
            .map((item) => item.value)
            .join(",");
          console.log(this.specsInfo, "表单对象");
          getSpecsAdd(this.specsInfo).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              //重新调用接口列表以及总条数
              this.getSpecs();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    //封装一个查询一条数据的接口
    lookup(id) {
      getSpecsInfo({ id }).then((res) => {
        if (res.code == 200) {
          this.specsInfo = res.list[0];
          // console.log(res,'回显数据');
          /* 
          后端返回的规格属性值是：
          [值1,值2,值3...]
          但是我们数据回显需要的数据格式是
          [{id:1,value:'值1'},{id:2,value:'值2'},{id:3,value:'值3'}....]
          */
          this.specsInfo.attrs.map((item, index) => {
            console.log(item, "数组中的每一项");
            //通过判断来区分数据回显的位置
            // this.attrArr 这个数组默认已经含有一项了，直接push会错位，所以要判断一下
            if (index == 0) {
              this.attrArr[0].id = item;
              this.attrArr[0].value = item;
            } else {
              this.attrArr.push({
                id: item,
                value: item,
              });
            }
          });
        }
      });
    },
    // 编辑事件
    editSpecs() {
      this.$refs.specsInfo.validate((valid) => {
        if (valid) {
          this.specsInfo.attrs = this.attrArr
            .map((item) => item.value)
            .join(",");
          seckedit(this.specsInfo).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              //重新调用接口列表
              this.getSpecs();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },

    changeCate(){
       this.specsInfo.second_cateid='';
        this.specsInfo.goodscountInput='';
      this.catelist.forEach(element => {
        if(element.id==this.specsInfo.first_cateid){
             this.catelist2=element.children;
        }
      });
    },
     changeCate1(){
              getGoodsList().then((res) => {
                this.goodscount=[];
                this.specsInfo.goodscountInput='';
                   console.log(res,'55555');
                  if (res.code == 200) {
                    res.list.forEach(i => {
                       if(i.second_cateid==this.specsInfo.second_cateid){
                         this.goodscount.push(i);
                       }
                    });
                    console.log(this.goodscount);
                  } else {
                    this.$message.error(res.msg);
                  }
                });
    },
  },
   mounted() {
     catelist().then((res) => {
            // console.log(res,'菜单添加');
            if (res.code == 200) {
              this.catelist=res.list;
             
            } else {
              this.$message.error(res.msg);
            }
          });
  }
};
</script>

<style scoped></style>
